/*
 * Copyright (C) 2011 xjuraj
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package sk.lieskove.jianghongtiao.websearch.document.preprocessing.actions;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.log4j.Logger;
import sk.lieskove.jianghongtiao.common.utils.FileUtils;
import sk.lieskove.jianghongtiao.common.utils.PropertiesUtils;
import sk.lieskove.jianghongtiao.common.utils.text.StringUtils;
import sk.lieskove.jianghongtiao.websearch.persistence.WebSearchResponse;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;

/**
 * 
 * @author xjuraj e-mail: jjurco.sk_gmail.com
 */
public class CleanText implements Serializable, PreprocessAction {
    
    private transient Logger log = Logger.getLogger(CleanText.class);
    private transient PropertiesUtils pu = new PropertiesUtils(CleanText.class);
    
    private CleanTextActionsEnum[] actions = {};

    public CleanText(CleanTextActionsEnum ... actions) {
        this.actions = actions;
    }

    @Override
    public WebSearchResponse preprocess(WebSearchResponse response) {
        log.debug("Try to extract HTML text. Html2Txt");
        if((response.getFile() == null) || (actions == null)){
            return response;
        }
        try {
            // create an instance of HtmlCleaner
            File outFile = FileUtils.getTempFile("CleanActions_", "");
            String result = org.apache.commons.io.FileUtils.readFileToString(
                    response.getFile(), "UTF-8");
            
            for (CleanTextActionsEnum action : actions) {
                switch(action){
                    case CHARACTERS_FOR_SPACE: 
                        result = replaceCharactersForSpace(result);
                        break;
                    case DECODE_HTML_ENTITIES:
                        result = decodeHtmlEntities(result);
                        break;
                    case REGEXP_REPLACE:
                        result = regexpReplace(result);
                        break;
                    case STRINGS_FOR_SPACE:
                        result = stringsForReplace(result);
                        break;
                    case TO_LOWER:
                        result = toLower(result);
                        break;
                    case REMOVE_TRAILING_SPACES:
                        result = removeTrailingSpaces(result);
                        break;
                    default: 
                        throw new IllegalArgumentException("Undefined "
                                + "CleanTextActionEnum type: "+action);
                }
            }
            FileUtils.write(outFile, "UTF-8", result);
            response.setFile(outFile);
        } catch (IOException ex) {
            log.error("I/O exception when trying to extract HTML text");
        }
        return response;
    }
    
    /**
     * replace following characters for the space: ~/,+©:()…»–|#[]{}·?•\"*<>–“„;®%.!
     * @param input string for replace
     * @return string without replaced characters
     */
    public static String replaceCharactersForSpace(String input){
        return input.replaceAll("[~\\/,+©:\\(\\)…»–\\|\\#\\[\\]{}·?•\\\\\"\\*<>–“„;®%\\.!]", " ");
    }
    
    /**
     * decode HTML entities in the string. See contract for method unescapeHtml(String)
     * in the class {@link org.apache.commons.lang.StringEscapeUtils}
     * @param input string to decode HTML entities
     * @return string without HTML entities
     */
    public static String decodeHtmlEntities(String input){
        return StringEscapeUtils.unescapeHtml(input);
    }

    /**
     * replace following regular expressions in the string for the space:
     * <ul>
     *   <li>( +\\.+ *\\.*) (dots and spaces in the string, no after word or splitting word)</li>
     *   <li>\.{2,65535} (more than one dot)</li>
     *   <li>&lt;\?xml version=.* encoding=.*\?&gt; (remaining XML declaration tags)</li>
     *   <li> -+</li>
     *   <li> —+</li>
     *   <li></li>
     * </ul>
     * @param result
     * @return 
     */
    public static String regexpReplace(String result) {
        return result.
                replaceAll("<\\?xml version=.* encoding=.*\\?>", " ").
                replaceAll("\\.{2,65535}", " ").
                //at least one space, one dot repeating
                replaceAll(" +[\\.\\-—]+ *[\\.\\-—]*", " ");
    }

    /**
     * Replace all following strings in the input text for spaces:
     * <ul>
     *   <li>&nbsp; (non-breaking space 
     *       <a href="http://www.fileformat.info/info/unicode/char/a0/index.htm">
     *       defined in Unicode Standard.</a>)
     *   </li>
     * </ul>
     * @param result
     * @return 
     */
    public static String stringsForReplace(String result) {
        return result.replace('\u00A0', ' ');
    }

    /**
     * Convert string to lover case string
     * @param result string to convert
     * @return  lower cased string
     */
    public static String toLower(String result) {
        return result.toLowerCase();
    }

    /**
     * remove all trailing spaces. See contract for 
     * {@link sk.lieskove.jianghongtiao.common.utils.text.StringUtils} method.
     * 
     * @param result string for removing trailing spaces
     * @return the same as after applying {@link sk.lieskove.jianghongtiao.common.utils.text.StringUtils} method.
     */
    public static String removeTrailingSpaces(String result) {
        return StringUtils.trailingSpaces(result);
    }
    
}
